package cn.tmpan.project.config;


import cn.tmpan.project.common.Common;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

import static com.beust.jcommander.internal.Lists.newArrayList;

/**
 * The type Swagger config.
 *
 * @author tieMinPan
 * @since 2019 -05-24 15:06
 */
@Configuration
public class SwaggerConfig {

    /**
     * The Swagger enabled.
     * <p>
     * 是否开启swagger ，正式环境一般是需要关闭的，可根据springboot的多环境配置进行设置
     */
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * Create rest api docket.
     *
     * @return the docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("小程序前端接口")
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("cn.tmpan.project.controller"))
                .paths(PathSelectors.regex("/api/.*"))
                .build()
                .pathMapping("/")
                .securitySchemes(securitySchemesByWeChat())
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket createRestApiByAdmin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("小程序管理端接口")
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("cn.tmpan.project.controller"))
                .paths(PathSelectors.regex("/admin/.*"))
                .build()
                .pathMapping("/")
                .securitySchemes(securitySchemesByAdmin())
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket createRestApiByOther() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("其他接口")
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("cn.tmpan.project.controller"))
                .paths(PathSelectors.regex("/sys/.*"))
                .build()
                .pathMapping("/")
                .apiInfo(apiInfo());
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目接口文档")
                /**
                 * 描述
                 */
                .description("tieMinPan")
                /**
                 * 作者信息
                 */
                .contact(new Contact("tieMinPan", "https://gitee.com/tmpan/", "1255070026@qq.com"))
                .version("1.0.0")
                .build();
    }

    private List<ApiKey> securitySchemesByAdmin() {
        return newArrayList(
                new ApiKey(Common.AUTH_HEADER_ADMIN, Common.AUTH_HEADER_ADMIN, "header"));
    }

    private List<ApiKey> securitySchemesByWeChat() {
        return newArrayList(
                new ApiKey(Common.AUTH_HEADER_USER, Common.AUTH_HEADER_USER, "header"));
    }
}
